library(tidycensus)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.5 ✓ purrr 0.3.4
## ✓ tibble 3.1.5 ✓ dplyr 1.0.7
## ✓ tidyr 1.1.4 ✓ stringr 1.4.0
## ✓ readr 2.0.2 ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(pander)
library(sf)
## Linking to GEOS 3.8.1, GDAL 3.2.1, PROJ 7.2.1
library(terra)
## terra version 1.4.11
##
## Attaching package: 'terra'
## The following object is masked from 'package:pander':
##
## wrap
## The following object is masked from 'package:dplyr':
##
## src
## The following object is masked from 'package:tidyr':
##
## extract
library(units)
## udunits database from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/units/share/udunits/udunits2.xml
library(purrr)
library(sp)
library(profvis)
library(ggmap)
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
##
## Attaching package: 'ggmap'
## The following object is masked from 'package:terra':
##
## inset
library(cartogram)
##
## Attaching package: 'cartogram'
## The following object is masked from 'package:terra':
##
## cartogram
library(patchwork)
##
## Attaching package: 'patchwork'
## The following object is masked from 'package:terra':
##
## area
library(tmap)
## Registered S3 methods overwritten by 'stars':
## method from
## st_bbox.SpatRaster sf
## st_crs.SpatRaster sf
library(viridis)
## Loading required package: viridisLite
library(tigris)
## To enable
## caching of data, set `options(tigris_use_cache = TRUE)` in your R script or .Rprofile.
##
## Attaching package: 'tigris'
## The following object is masked from 'package:tidycensus':
##
## fips_codes
library(gridExtra)
##
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
##
## combine
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggmap':
##
## wind
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
webshot::install_phantomjs()
## It seems that the version of `phantomjs` installed is greater than or equal to the requested version.To install the requested version or downgrade to another version, use `force = TRUE`.
state.df.sf (this has the spatial data of each state)
watershed
###temperature vs pitcher plant abundance by states
#take out places really far away
state.df.sf1 <- state.df.sf[!state.df.sf$STUSPS == "HI",]
state.df.sf1 <- state.df.sf[!state.df.sf$STUSPS == "PR",]
#number of pitchers in each state
state.df.sf1$pt_count <- lengths(st_intersects(state.df.sf1, pitcher.sf))
state.df.sf.f <- state.df.sf1 %>%
filter_at(vars(starts_with("pt_count")), any_vars(. > 0))
#plot of ranges of point counts in each state
p1 <- tm_shape(st_as_sf(state.df.sf.f)) + tm_polygons(col = "pt_count", border.col = "white") +
tm_legend(outside = TRUE) + tm_compass() +
tm_scale_bar() +tm_layout(main.title = "iNaturalist Purple Pitcher Plants", main.title.position = "center")
## working with predictor variables
class(state.df.sf.f$"Total.land")
## [1] "integer"
class(state.df.sf.f$"Total.cropland")
## [1] "integer"
state.df.sf.f$Total.cropland <- as.integer((state.df.sf.f$Total.cropland))
class(state.df.sf.f$"Total.cropland")
## [1] "integer"
state.df.sf.f <- mutate(state.df.sf.f, per_crop = Total.cropland/Total.land)
p2 <- tm_shape(st_as_sf(state.df.sf.f)) + tm_polygons(col = "per_crop", border.col = "white") + tm_legend(outside = TRUE) + tm_compass() +
tm_scale_bar() +tm_layout(main.title = "Proportion Cropland", main.title.position = "center")
tmap_arrange(p1, p2, nrow = 1)
## Scale bar set for latitude km and will be different at the top and bottom of the map.
## Scale bar set for latitude km and will be different at the top and bottom of the map.
temp <- state.df.sf.f$temp
plot_ly(state.df.sf.f) %>%
add_sf(
color = ~pt_count,
split = ~NAME,
span = I(1),
text = ~paste(NAME, per_crop, temp, mean_elevation),
hoverinfo = "text",
hoveron = "fills"
) %>%
layout(showlegend = FALSE) %>%
colorbar(title = "Pitcher Plant \n Count")
## No trace type specified:
## Based on info supplied, a 'scatter' trace seems appropriate.
## Read more about this trace type -> https://plotly.com/r/reference/#scatter
##ggplot plotting crop land percent and pitcher plant count per state
ggplot(state.df.sf.f, aes(x = per_crop, y=pt_count)) + geom_point() +
labs(x="Proportion Cropland", y="iNat S. purpurea observations") + geom_smooth(method = "lm")
## `geom_smooth()` using formula 'y ~ x'
##ggplot plotting annual mean temperature per state vs pitcher plant count
temp.extract.st <- terra::extract(temp.rast, state.df.sf.f, fun = mean, na.rm=TRUE)
state.df.sf.f$temp <- temp.extract.st
ggplot(state.df.sf.f, aes(x = temp, y=pt_count)) + geom_point() +
labs(x="Annual Mean Temperature (C*10)", y="iNat S. purpurea observations") + geom_smooth(method = "lm")
## `geom_smooth()` using formula 'y ~ x'
##ggplot plotting elevation and pitcher plant count per state
ggplot(state.df.sf.f, aes(x = mean_elevation, y=pt_count)) + geom_point() +
labs(x="Mean Elevation (m)", y="iNat S. purpurea observations") + geom_smooth(method = "lm")
## `geom_smooth()` using formula 'y ~ x'
hist(state.df.sf.f$mean_elevation)
#st.sp <- as(state.df.sf, 'Spatial')
#temp.crop <- raster::crop(temp.rast, st.sp)
#plot(temp.crop, axes = FALSE)
#plot(temp.crop, add = TRUE, box = FALSE, axes = FALSE)
#st_crs(state.df.sf) == st_crs(temp.rast)
#plot(st_geometry(state.df.sf))
#plot(temp.rast)
#e <- extent(c(-179.1743, -50, 20, 71.41377))
#t.c <- crop (temp.rast, e)
#plot(t.c)
#tm_shape(temp.crop) +
# tm_raster("Temp", palette = viridis(n=50), n=50, legend.show=FALSE, #legend.hist = TRUE, #legend.hist.title = "Mean Annual Temperature")